package main.java.pers.seg.model.utils;

import main.java.pers.seg.model.constants.PathConstants;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

import static main.java.pers.seg.model.constants.ObjectConstants.*;
import static main.java.pers.seg.model.constants.StringConstants.*;
import static main.java.pers.seg.model.constants.StringConstants.mainActivity;
import static main.java.pers.seg.model.constants.StringConstants.swipeEvent;
import static main.java.pers.seg.model.utils.FileUtils.*;
import static main.java.pers.seg.model.utils.JudgeUtils.eventAndHandler;
import static main.java.pers.seg.model.utils.ListUtils.*;
import static main.java.pers.seg.model.utils.NodeUtils.findParentScreen;
import static main.java.pers.seg.model.utils.NodeUtils.judgeElement;
import static main.java.pers.seg.model.utils.RuleUtils.implicitJudge;
import static main.java.pers.seg.model.utils.StringUtils.*;

public class JudgeUtils {

    private static boolean flag = false;
    public static Element activity(Element gatornode,Element ifmlnode){
            //在root节点下创建一个节点
            Element ifmlElement = ifmlnode.addElement("Element");
            ifmlnode = ifmlElement;
            String name = gatornode.attributeValue("name");
            //给节点添加属性
            ifmlElement.addAttribute("name", name);
            if(gatornode.getName().equals("OtherWindow")){
                ifmlElement.addAttribute("xsi:type", "ext:Drawer");
            }else{ //Activity,Fragment
                ifmlElement.addAttribute("xsi:type", "ext:Screen");
            }
        //ifmlElement.addAttribute("xsi:type", "ext:Screen");

        ifmlElement.addAttribute("inInteractionFlows", "");
            judgeElement(ifmlnode);
            //隐性规则
            if(name.equals("org.asdtm.goodweather.MainActivity")){
                implicitJudge(gatornode, ifmlnode, ifmlElement, "org.asdtm.goodweather.MainActivity");
            }
            if(name.equals("android.support.design.widget.NavigationView")){
                implicitJudge(gatornode, ifmlnode, ifmlElement, "android.support.design.widget.NavigationView");
            }
        return ifmlnode;
    }
    public static Element viewActivity(Element gatornode,Element ifmlnode){
        Element ifmlElement = ifmlnode.addElement("Element");
        ifmlnode = ifmlElement;
        //给节点添加属性
        ifmlElement.addAttribute("xsi:type", "ext:Screen");
        ifmlElement.addAttribute("inInteractionFlows", "");
        ifmlElement.addAttribute("name", gatornode.attributeValue("type"));
        judgeElement(ifmlnode);

        return ifmlnode;
    }
    public static Element view(Element gatornode, Element ifmlnode){
        String type = gatornode.attributeValue("type");
        String id = gatornode.attributeValue("id");
        String name = gatornode.attributeValue("idName");
            //TODO 可以封装为特殊情况
            //view生成screen的情况1
            if (type.equals("android.support.v4.view.ViewPager")) {
                ifmlnode = viewActivity(gatornode, ifmlnode);
                //islandmark和isxor这两个属性设为true
                ifmlnode.addAttribute("isLandmark", "true");
                ifmlnode.addAttribute("isXOR", "true");
            }
            //view生成screen的情况2
            if (type.equals("android.view.preference-headers")) {
                ifmlnode = viewActivity(gatornode, ifmlnode);
                ifmlnode.addAttribute("isDefault", "true");
                Element element = findParentScreen(ifmlnode);
                element.addAttribute("isXOR", "true");
            }
        //判断type是viewcontainer还是viewcomponents,若为viewcontainer 调activity方法；
        if(viewcontainermap.containsKey(type)
                && !type.equals("android.support.design.widget.NavigationView")){//过滤<View type="android.support.design.widget.NavigationView" id="2131689605" idName="navigation_view">
            Element ifmlElement = ifmlnode.addElement("Element");
            ifmlnode = ifmlElement;
            ifmlElement.addAttribute("name", name);
            ifmlElement.addAttribute("xsi:type",viewcontainermap.get(type));
            ifmlElement.addAttribute("inInteractionFlows", "");
            if(type.equals("android.support.v7.widget.Toolbar") ||
                    (type.equals("android.view.Menu"))){
                ifmlElement.addAttribute("isLandmark", "true");
            }
            judgeElement(ifmlnode);
        }
        if (filtermap.containsKey(type) && !viewcontainermap.containsKey(type)) {   //如果是viewcomponent嵌套viewcomponent 就废弃
            Element ifmlElement = ifmlnode.addElement("Elements");
            String ce_type = filtermap.get(gatornode.attributeValue("type"));

            //对type进行分类
            classifyType(ifmlElement,name,ce_type);

            if (type.equals("android.support.v7.widget.SearchView")) {
                ifmlElement.addAttribute("id", "GO");
                ifmlElement.addAttribute("name", "Form");
                Element ifmlElement2 = ifmlElement.addElement("viewComponentParts");
                ifmlElement2.addAttribute("xsi:type", simpleField);
                ifmlElement2.addAttribute("id", name);
                ifmlElement2.addAttribute("name", name);
                ifmlnode = ifmlElement2;
            }
            //暂时不作处理
            /*else if (type.equals("android.support.v7.widget.RecyclerView")) {
                Element ifmlElement2 = ifmlElement.addElement("viewComponentParts");
                ifmlElement2.addAttribute("xsi:type", selectionField);
                ifmlElement2.addAttribute("name", "");
                ifmlnode = ifmlElement2;
            }*/

            //处理id
            else{
                classifyId(gatornode, ifmlElement, type, name);
            }

           if (judgeElement(ifmlElement) == false) {
                ifmlnode.remove(ifmlElement);//删除节点
            } else {
                ifmlnode = ifmlElement;
            }

        } else if (type.equals("android.support.v4.widget.SwipeRefreshLayout")) {
            Element ifmlElement = eventAndHandler(ifmlnode, scrollEvent, downScroll);

            ifmlnode = transfer(ifmlElement, mainActivity).getParent();
        } else if (type.equals("android.support.v4.widget.DrawerLayout")) {
            Element ifmlElement = eventAndHandler(ifmlnode, swipeEvent, rightSwipe);

            ifmlnode = transfer(ifmlElement, sideBar).getParent();
        }
            //这部分暂时不处理
        /*else if (attribute.getValue().equals("android.support.design.widget.NavigationView")) {
            Element ifmlElement = eventAndHandler(ifmlnode,swipeEvent,leftSwipe);
            ifmlnode = transfer(ifmlElement,mainActivity).getParent();
        }*/

        return ifmlnode;
    }

    public static Element eventAndHandler(Element gatornode,Element ifmlnode ,String type) {
        String handler =gatornode.attributeValue("handler");
            if(handler.indexOf("onQueryTextChange")!=-1){
                ifmlnode =eventAndHandler(ifmlnode,onInputEvent,"OnInput Event");
            }
            else if(handler.indexOf("onQueryTextSubmit")!=-1){
                ifmlnode =eventAndHandler(ifmlnode,onSubmitEvent,"OnSubmit Event");
            }
            else if(!type.equals("android.support.v7.widget.RecyclerView")
                    && !type.equals("android.support.v7.widget.SearchView")){
                Element ifmlElement = ifmlnode.addElement("viewElementEvents");
                ifmlElement.addAttribute("xsi:type", touchEvent);
                ifmlElement.addAttribute("name", gatornode.attributeValue("event"));
                ifmlnode = ifmlElement;
            }
            else if(type.equals("android.support.v7.widget.RecyclerView")){
                Element ifmlElement = ifmlnode.addElement("viewElementEvents");
                ifmlElement.addAttribute("xsi:type", onSelectEvent);
                ifmlElement.addAttribute("name", gatornode.attributeValue("event"));
                ifmlnode = ifmlElement;
            }
        return ifmlnode;
    }

    public static Element eventAndHandler(Element ifmlnode, String type, String name) {
        Element ifmlElement = ifmlnode.addElement("viewElementEvents");
        ifmlElement.addAttribute("xsi:type", type);
        ifmlElement.addAttribute("name", name);

        return ifmlElement;
    }

    public static Element transfer(Element gatornode, Element ifmlnode) {
        String target="";
        if(gatornode.attribute("target")!=null){
            target = gatornode.attributeValue("target");
        }
        else if(gatornode.attribute("Target")!=null){
            target = gatornode.attributeValue("Target");
        }
        Element ifmlElement = ifmlnode.addElement("outInteractionFlows");
        ifmlElement.addAttribute("xsi:type", navigationFlow);
        ifmlElement.addAttribute("targetInteractionFlowElement", targetInteractionFlowElement + getIndex(target));
        ifmlnode = ifmlElement;

        return ifmlnode;
    }
    public static Element transfer(Element ifmlElement , String activity){
        Element ifElement = ifmlElement.addElement("outInteractionFlows");
        ifElement.addAttribute("xsi:type", navigationFlow);
        ifElement.addAttribute("targetInteractionFlowElement", targetInteractionFlowElement + getActivity(activity));

        return ifmlElement;
    }

    public static Element transfer(Element ifmlElement, String type , String source) {
        String target = type +" "+source;
        Element ifElement = ifmlElement.addElement("outInteractionFlows");
        ifElement.addAttribute("xsi:type", navigationFlow);
        ifElement.addAttribute("targetInteractionFlowElement", targetInteractionFlowElement + getIndex(target));
        ifmlElement = ifElement;

        return ifmlElement;
    }
}



